﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using YiDingCanWp7.Models;
using Newtonsoft.Json;

namespace YiDingCanWp7
{
    public partial class ShopFeedbackPage : PhoneApplicationPage
    {
        #region 属性
        string AddFeedbackUrl = BaseConfig.BaseUrl + "/Shop/AddShopFeedback";
        string AddItemUrl = BaseConfig.BaseUrl + "/Shop/UserAddItem";
        string ItemUnits = BaseConfig.BaseUrl + "/RestApi/api/Shop/ItemUnits";
        IEnumerable<string> units = new List<string>();
        public string shopid = string.Empty;
        #endregion
        public ShopFeedbackPage()
        {
         
            InitializeComponent();
            performanceProgressBar.IsIndeterminate = false;
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            shopid = this.NavigationContext.QueryString["shopid"].ToString();
            if (!string.IsNullOrEmpty(shopid)&&units.Count()==0)
            {
                WebClient webClient = new WebClient();
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers["Accept-type"] = "application/json";
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClientUnit_DownloadStringCompleted);
                webClient.DownloadStringAsync(new Uri(ItemUnits));
      
            }
            base.OnNavigatedTo(e);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            performanceProgressBar.IsIndeterminate = true;
            var webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            ShopFeedback feedback = new ShopFeedback()
            {
               Content=FeedBackContent_TextBox.Text.Trim(),
               Contact=FeedBackContact_TextBox.Text.Trim(),
               DeviceUniqueId = PhoneUtils.GetDeviceUniqueId()
            };
            var json = JsonConvert.SerializeObject(feedback);
            webClient.UploadStringAsync(new Uri(AddFeedbackUrl), "POST", json);
        }

        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) {
            try
            {
                MessageBox.Show(e.Result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                performanceProgressBar.IsIndeterminate = false;
            }
        }
        void webClientUnit_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                units = JsonConvert.DeserializeObject<List<string>>(e.Result);
                listBox_category.ItemsSource = units;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            #region 检测收入数据
            if (string.IsNullOrEmpty(DishName_TextBox.Text.Trim()))
            {
                MessageBox.Show("请输入商品的名称！");
                return;
            }
            if (string.IsNullOrEmpty(DishPrice_TextBox.Text.Trim()))
            {
                MessageBox.Show("请输入商品的价格！");
                return;
            } 
            if (string.IsNullOrEmpty((string)listBox_category.SelectedItem))
            {
                MessageBox.Show("请输入商品的单位！");
                return;
            }
            #endregion
            performanceProgressBar.IsIndeterminate = true;
            var webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(AddItemwebClient_UploadStringCompleted);
            Item item = new Item()
            {
               Title=DishName_TextBox.Text.Trim(),
               Price=decimal.Parse(DishPrice_TextBox.Text.Trim()),
               DeviceUniqueId = PhoneUtils.GetDeviceUniqueId(),
               ShopId=int.Parse(shopid)
            };
            var json = JsonConvert.SerializeObject(item);
            webClient.UploadStringAsync(new Uri(AddItemUrl), "POST", json);
        }

        void  AddItemwebClient_UploadStringCompleted (object sender, UploadStringCompletedEventArgs e){
            try
            {
                MessageDTO message = JsonConvert.DeserializeObject<MessageDTO>(e.Result);
                MessageBox.Show(message.message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                performanceProgressBar.IsIndeterminate = false;
            }
        }

        private void appbar_button1_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddShopPicture.xaml?shopid="+shopid, UriKind.RelativeOrAbsolute));
        }

        private void appbar_button2_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddShopSellerCat.xaml?shopid="+shopid, UriKind.RelativeOrAbsolute));
        }
    }
}